package com.example.tourapp.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.tourapp.Adapter.Telephone_Apadter;
import com.example.tourapp.Pojo.Pojo_telephone;
import com.example.tourapp.R;

import java.util.ArrayList;
import java.util.List;

public class Telephone extends AppCompatActivity {
    private EditText t_edtext;
    private ListView t_listview;
    private List<Pojo_telephone>list;

    private final String[] phonename = { "武汉市旅游投诉电话", "湖北省旅游投诉电话", "国家旅游投诉电话",
            "武汉市假日办_值班电话", "武汉市假日办_投诉电话", "武昌火车站", "汉口火车站", "新华路汽车客运站",
            "金家墩汽车客运站", "青年路汽车客运站", "宏基汽车客运站", "汉阳长途汽车客运站", "汉阳客运中心", "天河机场",
            "武汉客运港长途汽车站", "武汉市110联动投诉", "江岸区110联动办", "硚口区110联动办", "汉阳区110联动办",
            "武昌区110联动办", "洪山区110联动办", "青山区110联动办", "东湖开发区110联动办", "新洲区110联动办" };
    private final String[] phonenumber = { "02782855773", "02787124701",
            "01065275315", "02782761686", "02782855773", "02788068888",
            "65650666", "85870482", "85870482", "85731761", "85731761",
            "84842395", "68841281", "83666666", "82857625", "8532110",
            "82410810", "83798277", "84843446", "88851360", "87393996",
            "688865375", "87491700", "86912345" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telephone);
        initView();
        initfunction();
    }

    private void initView() {
        t_edtext = (EditText) findViewById(R.id.t_edtext);
        t_listview = (ListView) findViewById(R.id.t_listview);
    }
    private void initfunction() {
        list=new ArrayList<>();

        for (int i = 0; i < phonename.length; i++) {
            list.add(new Pojo_telephone(phonename[i],phonenumber[i]));
        }
        Telephone_Apadter apadter=new Telephone_Apadter(list,this);
        t_listview.setAdapter(apadter);

        t_edtext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String edtext = charSequence.toString();
              List<Pojo_telephone> list_data=new ArrayList<>();
                for(int j=0;j<phonenumber.length;j++){
                    if(phonenumber[j].contains(edtext)){
                        //重新添加到新的集合中
                        list_data.add(new Pojo_telephone(phonename[j],phonenumber[j]));
                    }
                    Telephone_Apadter apadter=new Telephone_Apadter(list_data,Telephone.this);
                    t_listview.setAdapter(apadter);
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }


}